package com.android.fragmentdemo.definenestedscroll;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.view.NestedScrollingParent;
import androidx.core.view.NestedScrollingParent2;
import androidx.core.view.NestedScrollingParent3;
import androidx.core.view.NestedScrollingParentHelper;
import androidx.core.widget.NestedScrollView;
import com.android.fragmentdemo.defineviews.NestedScrollRecyclerView;

public class TaNestedScrollParent extends LinearLayout implements NestedScrollingParent {
    private AppCompatImageView mImageView;
    private AppCompatTextView mTextView;
    //private TaNestedScrollChild mScrollChild;
    private NestedScrollRecyclerView mScrollChild;
    private final NestedScrollingParentHelper mParentHelper;

    private int mIvHeight;
    private int mTvHeight;

    public TaNestedScrollParent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mParentHelper = new NestedScrollingParentHelper(this);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mImageView = (AppCompatImageView) getChildAt(0);
        mTextView = (AppCompatTextView) getChildAt(1);
        mScrollChild = (NestedScrollRecyclerView) getChildAt(2);

        mImageView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
            if (mIvHeight <= 0) {
                mIvHeight = mImageView.getMeasuredHeight();
            }
        });

        mTextView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
            if (mTvHeight <= 0) {
                mTvHeight = mTextView.getMeasuredHeight();
            }
        });
    }

    @Override
    public boolean onStartNestedScroll(@NonNull View child, @NonNull View target, int nestedScrollAxes) {
        //return super.onStartNestedScroll(child, target, nestedScrollAxes);
        return target instanceof TaNestedScrollChild;
    }

    @Override
    public void onNestedScrollAccepted(@NonNull View child, @NonNull View target, int axes) {
        super.onNestedScrollAccepted(child, target, axes);
        mParentHelper.onNestedScrollAccepted(child, target, axes);
    }

    @Override
    public void onStopNestedScroll(@NonNull View child) {
        super.onStopNestedScroll(child);
        mParentHelper.onStopNestedScroll(child);
    }

    @Override
    public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed) {
        super.onNestedPreScroll(target, dx, dy, consumed);
        //先于child滚动 前三个参数为输入  最后一个为输出
        if (hideImageView(dy) || showImageView(dy)) {
            scrollBy(0, -dy);
            consumed[1] = dy;
        }
    }

    @Override
    public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        //后于child滚动
    }

    @Override
    public boolean onNestedPreFling(@NonNull View target, float velocityX, float velocityY) {
        //是否消费了fling
        return false;
    }

    @Override
    public boolean onNestedFling(@NonNull View target, float velocityX, float velocityY, boolean consumed) {
        //是否消费了fling
        return false;
    }

    @Override
    public int getNestedScrollAxes() {
        return mParentHelper.getNestedScrollAxes();
    }

    @Override
    public void scrollTo(int x, int y) {
        y = Math.max(0, Math.min(y, mIvHeight));
        super.scrollTo(x, y);
    }

    public boolean showImageView(int dy) {
        if (dy > 0) {
            return getScrollY() > 0 && mScrollChild.getScrollY() == 0;
        }
        return false;
    }

    public boolean hideImageView(int dy) {
        if (dy < 0) {
            return getScrollY() < mIvHeight;
        }
        return false;
    }
}
